package com.wsd.action.admin;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.TreeUI;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.wsd.action.BaseAction;
import com.wsd.bean.Admin;
import com.wsd.bean.Category;
import com.wsd.service.SysService;
import com.wsd.service.UserService;
import com.wsd.util.SysUtil;
import com.wsd.util.TreeUtil;

public class SysAction extends BaseAction {
	private SysService sysService;
	private Admin admin;
	private String newPassword;
	private String oldPassword;

	public String login(){
		admin = sysService.login(admin);
		
		if(admin != null){
			process(admin);
			result.put("loginResult", true);
			result.put("loginMsg", "登录成功");
			result.put("admin", admin);
			SysUtil.login(admin);
			success = true;
		}else{
			//密码错误 或者 用户名错误
			result.put("loginResult", false);
			result.put("loginMsg", "登录失败，用户名或密码错误");
			success = false;
		}
		
		return AJAX;
	}
	
	public String logout(){
		SysUtil.logout();
		return AJAX;
	}
	
	public String modifyPassw(){
		Admin admin = SysUtil.currentAdmin();
		String msg = "";
		
		if(admin == null){
			msg = "修改密码失败，登录超时";
			success = false;
		}else if(!admin.getAdminPass().equals(oldPassword)){
			msg = "原密码错误";
			success = false;
		}else{
			admin.setAdminPass(newPassword);
			sysService.updateAdmin(admin);
			msg = "修改成功";
		}
		result.put("msg", msg);
		return AJAX;
	}
	
	/**
	 * @param admin2
	 */
	private void process(Admin admin) {
		admin.setArticleses(null);
		admin.setMessages(null);
		admin.setProductOrders(null);
	}

	@JSON(serialize = false)
	public SysService getSysService() {
		return sysService;
	}
	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}
	@JSON(serialize = false)
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	@JSON(serialize = false)
	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	@JSON(serialize = false)
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
	
	

}
